<?php
declare(strict_types=1);

namespace App\ZxAdmin\Content;

use App\Controller;
use Field\SysAlbumAttachConst;
use Field\SysApiStatRow;
use Field\SysCateConst;
use Framework\Libs\Model;

// hook controller_zxadmin_content_cate_use.php

/**
 * @title   分类管理
 * @auth    true
 * @login   true
 * @menu    true
 * @component vlist/index.vue
 * @icon el-icon-menu
 * @rank    1
 */
class Cate extends Controller
{
    // hook controller_zxadmin_content_cate_public_start.php

    // hook controller_zxadmin_content_cate_public_end.php
    public const Conf = [
        'Index' => 'CateID',
        'TreeData' => true,
        'rowField' => 'CateID',
        'parentField' => 'ParentCateID',
        'pageSize' => 25,
        'pageSizes' => [10, 15, 20, 25, 30, 50, 100, 200, 500],
        'remoteSort' => 0,
        'remoteFilter' => 0,
        'remoteSummary' => 0,
        'hidePagination' => 1,
        'hideRefresh' => 0,
        'hideSetting' => 0,
        'hideDo' => 0,
        'add' => 1,
        'start' => 1,
        'stop' => 1,
        'delete' => 1,
        'import' => 0,
        'export' => 0
    ];
    /**
     * @title   列操作
     */
    public const Tools = [];
    /**
     * @title   列配置
     */
    public const Columns = [['table' => 'SysDict.1', 'prop' => 'DictID', 'label' => '', 'hidden' => 1, 'options' => ['remote' => ['api' => '', 'name' => ''], 'items' => '']], ['table' => 'SysDict.1', 'prop' => 'DictName', 'label' => '字典名字', 'hidden' => 1, 'options' => ['remote' => ['api' => '', 'name' => ''], 'items' => ''], 'add' => 1, 'edit' => 1, 'field' => 1, 'search' => 1], ['table' => 'SysDict.1', 'prop' => 'Content', 'label' => '字典配置', 'hidden' => 1, 'options' => ['remote' => ['api' => '', 'name' => ''], 'items' => ''], 'edit' => 1], ['table' => 'SysDict.1', 'prop' => 'Status', 'label' => '状态(1:禁用,2:启用]', 'hidden' => 1, 'options' => ['remote' => ['api' => '', 'name' => ''], 'items' => ''], 'edit' => 1, 'field' => 1, 'search' => 1]];
    /**
     * @title   筛选
     */
    public const Filter = [];
    /**
     * @title   添加内容
     */
    public const AddForm = [['table' => 'SysDict.1', 'prop' => 'DictName', 'label' => '字典名字', 'hidden' => 1, 'options' => ['remote' => ['api' => '', 'name' => ''], 'items' => ''], 'add' => 1]];
    /**
     * @title   编辑内容
     */
    public const EditForm = [['table' => 'SysDict.1', 'prop' => 'DictName', 'label' => '字典名字', 'hidden' => 1, 'options' => ['remote' => ['api' => '', 'name' => ''], 'items' => ''], 'add' => 1, 'edit' => 1], ['table' => 'SysDict.1', 'prop' => 'Content', 'label' => '字典配置', 'hidden' => 1, 'options' => ['remote' => ['api' => '', 'name' => ''], 'items' => ''], 'edit' => 1], ['table' => 'SysDict.1', 'prop' => 'Status', 'label' => '状态(1:禁用,2:启用]', 'hidden' => 1, 'options' => ['remote' => ['api' => '', 'name' => ''], 'items' => ''], 'edit' => 1]];

    /**
     * @title  数据库连接
     * @rank    99
     */
    public function action_Conf_PUT()
    {
        // hook controller_zxadmin_content_cate_index_start.php
        $Columns = [
            [
                'label' => "操作",
                'type' => "button",
                'options' => [
                    ['Name' => '子类', "Do" => "Add", 'Icon' => 'el-icon-plus'],
                ],
                'width' => "50"
            ],
            [
                'label' => "分类ID",
                'prop' => "CateID",
                'width' => "100"
            ],
            [
                'type' => "input",
                'label' => "分类名称",
                'prop' => "Name",
                'editable'=>1,
                'width' => "200"
            ],
            [
                'type' => "image",
                'label' => "图标",
                'prop' => "AttachID",
                'editable'=>1,
                'width' => "60"
            ],
            [
                'type' => "input",
                'label' => "排序",
                'prop' => "Rank",
                'editable'=>1,
                'width' => "60"
            ],
            [
                'type' => "dict",
                'label' => "状态",
                'prop' => "Status",
                'editable'=>1,
                'width' => "60"
            ],
            [
                'type' => "table",
                'label' => "权限",
                'prop' => "Accesson",
                'width' => "60",
                'editable'=>1,
                'apiObjRemote'=>'/zxadmin/content/cateaccess'
            ],
            [
                'type' => "remoteSelect",
                'label' => "绑定商品",
                'prop' => "ProductID",
                'width' => "150",
                'editable'=>1,
                'apiObjRemote'=>'/zxadmin/content/page/index',
                'options'=>[]
            ],
            [
                'type' => "remoteSelect",
                'label' => "绑定单页",
                'prop' => "PageID",
                'width' => "150",
                'editable'=>1,
                'apiObjRemote'=>'/zxadmin/content/page/index',
                'options'=>[]
            ]
            ];

        $tabs = [];
        foreach ($_ENV['sys']['Cate'] as $k => $v) {
            $tabs[] = [
                'Name' => $v['name'],
                'Index' => $k
            ];
        }
        // hook controller_zxadmin_content_cate_index_end.php

        $this->response('0000', ['Tabs' => $tabs, 'Conf' => self::Conf, 'Filter' => self::Filter, 'Tools' => self::Tools, 'Columns' => $Columns]);

    }

    /**
     * @title  数据表
     * @rank    99
     */
    public function action_Index()
    {
        // hook controller_zxadmin_content_cate_index_start.php
        $group = $this->request->param('tab', 'pages');
        empty($group) and $group = 'pages';
        $cate = $this->SysCate->GetList([SysCateConst::GroupAt => $group]);

        $AttachID = !empty($cate['data']) ? arrlist_values($cate['data'],SysAlbumAttachConst::AttachID):[];

        $Attachs = !empty($AttachID) ? $this->SysAlbumAttach->select([SysAlbumAttachConst::AttachID=>$AttachID],[],0,0,SysAlbumAttachConst::AttachID.','.SysAlbumAttachConst::Patch,SysAlbumAttachConst::AttachID):[];
        foreach ($cate['data'] as &$row){
            $row[SysAlbumAttachConst::Patch]='//'._CONF('OssHost').( (!empty($row[SysAlbumAttachConst::AttachID]) && !empty($Attachs[$row[SysAlbumAttachConst::AttachID]])) ? $Attachs[$row[SysAlbumAttachConst::AttachID]][SysAlbumAttachConst::Patch]:'/logo.png');
        }
        //var_dump($this->SysCate->DataSuperior);
        //$cate['data'] = arraylist_tree($cate['data'], SysCateConst::CateID, SysCateConst::ParentCateID);
        // hook controller_zxadmin_content_cate_index_end.php
        $this->response('0000', $cate);
    }

    /**
     * @title   新增配置
     * @rank    72
     */
    public function action_Add_PUT()
    {
        // hook controller_zx_admin_sys_sys_dict_add_put_start.php

        $tab = $this->request->param('tab');
        $CateID = $this->request->param('CateID',0);
        $data=[
            [
                'label'=> "分类名称",
                'prop'=> "Name",
                'span'=> 24,
                'component'=> 'input',
                'options'=> ['maxlength'=>32,'placeholder'=>'请输入字典名'],
                'rules'=> [
                    ['required'=>true,'message'=>'请输入字典名']
                ],
                'requiredHandle'=> "$.required==true"
            ],
            [
                'label'=> "tab",
                'prop'=> "tab",
                'value'=>$tab,
                'span'=> 24,
                'component'=> 'hidden'
            ],
            [
                'label'=> "分类ID",
                'prop'=> "CateID",
                'value'=>$CateID,
                'span'=> 24,
                'component'=> 'hidden'
            ]
        ];

        // hook controller_zx_admin_sys_sys_dict_add_put_end.php
        $this->response('0000', ['data' => $data,'title'=>'添加字典']);
    }

    /**
     * @title   新增提交
     * @rank    71
     */
    public function action_Add_POST()
    {
        // hook controller_zx_admin_sys_sys_dict_add_post_start.php
        $data = [];
        $data['GroupAt']=$this->request->param('tab');
        $data['Name']=$this->request->param('Name');
        $CateID=$this->request->param('CateID',0);
        $this->CheckEmpty([$data['Name'],$data['GroupAt']],['分类名称','分组']);
        if(!empty($CateID)){
            $Parent = $this->SysCate->read_by_pk($CateID);
            empty($Parent['CateID']) && $this->response('0003',[],'分类不存在');
            $data['ParentCateID']=$CateID;
        }
        $r = $this->SysCate->insert($data);
        // make addformpost SysDict ZxAdmin\Sys
        // hook controller_zx_admin_sys_sys_dict_add_post_end.php
        $this->response($r ? '0000' : '0001', ['data' => $r]);
    }

    /**
     * @title   编辑字段
     * @rank    60
     */
    public function action_Field_POST()
    {
        // hook controller_zxadmin_content_cate_field_post_start.php
        $cond = [];
        $cond['CateID'] = $this->request->param('CateID', 0);
        $field = $this->request->param('field', '');
        $value = $this->request->param('value', '');
        $this->CheckEmpty([$field], ['待修改字段']);
        $field_fields = ['Name', 'ShortName', 'Status', 'Rank', 'Accesson', 'AttachID'];
        !in_array($field, $field_fields, true) && $this->response('0001', [], '字段不允许修改');
        !in_array($field, SysCateConst::fields, true) && $this->response('0001', [], '字段不允许修改');
        $update = [];
        switch ($field) {
            case 'Name':
            case 'ShortName':
                $update[$field] = $value;
                break;
            case 'Rank':
            case 'AttachID':
            case 'Accesson':
            case 'Status':
                $update[$field] = intval($value);
                break;
        }

        $r = !empty($update) ? $this->SysCate->update($cond, $update) : false;
        // hook controller_zxadmin_content_cate_field_post_end.php
        $this->response($r ? '0000' : '0001', ['data' => $r]);
    }


    /**
     * @title   删除数据
     * @rank    50
     */
    public function action_Index_Delete()
    {
        // hook controller_zxadmin_content_cate_delete_start.php
        $data = $this->request->param('data', []);
        $this->CheckEmpty([$data], ['操作数据为空']);
        $r = $this->SysCate->deleteByIndex($data);
        // hook controller_zxadmin_content_cate_delete_end.php
        $this->response($r ? '0000' : '0001', ['data' => $r]);
    }


    /**
     * @title   启用
     * @rank    41
     */
    public function action_Enable()
    {
        // hook controller_zxadmin_content_cate_enable_start.php
        $data = $this->request->param('data', []);
        $this->CheckEmpty([$data], ['操作数据为空']);
        $r = $this->SysCate->updateByIndex($data, [SysCateConst::Status => 1]);
        // hook controller_zxadmin_content_cate_enable_end.php
        $this->response($r ? '0000' : '0001', ['data' => $r]);
    }

    /**
     * @title   禁用
     * @rank    40
     */
    public function action_Disable()
    {
        // hook controller_zxadmin_content_cate_disable_start.php
        $data = $this->request->param('data', []);
        $this->CheckEmpty([$data], ['操作数据为空']);
        $r = $this->SysCate->updateByIndex($data, [SysCateConst::Status => 0]);
        // hook controller_zxadmin_content_cate_disable_end.php
        $this->response($r ? '0000' : '0001', ['data' => $r]);
    }

    // hook controller_sys_api_stat_end.php
}

?>